﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Actuals.Business;
using Actuals.CoreDomain.Business;
using Actuals.CoreDomain.Data;
using Actuals.CoreDomain;
using Actuals.Common;

namespace Actuals
{
    public partial class ShowTeamMembers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                IBusinessTeam businessTeam = new BusinessTeam();
                IList<ITeam> teams = businessTeam.GetTeams();
                ddlTeam.DataSource = teams;
                ddlTeam.DataTextField = "TeamDescription";
                ddlTeam.DataValueField = "TeamId";
                ddlTeam.DataBind();
            }
            else
            {
                if (Request.Form["__EVENTTARGET"] == "refresh")
                {
                    BindGridView();
                }
            }
            this.Page.ClientScript.GetPostBackEventReference(lblMsg, string.Empty);

        }
        protected void BindGridView()
        {
            int teamId = (int.Parse(ddlTeam.SelectedValue));
            IBusinessTeamMember businessTeamMember = new BusinessTeamMember();
            IList<ITeamMember> teamMembers = businessTeamMember.GetTeamMembers(teamId);
            ViewState["DataSource"] = teamMembers;
            gvResults.DataSource = teamMembers;
            gvResults.DataBind();
        }
        protected void btnGetTeamMembers_Click(object sender, EventArgs e)
        {
            BindGridView();
        }

        protected void gvResults_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            string teamMemberId;
            switch (e.CommandName)
            {
                case "EditTeamMember":

                    teamMemberId = e.CommandArgument.ToString();
                    ClientScript.RegisterStartupScript(this.GetType(), "UpdateAddTeamMember", "UpdateAddTeamMember(' " + teamMemberId + " ');", true);
                    break;



            }

        }
        protected string getEmpName(object employeeId)
        {


            IBusinessEmployee businessEmployee = new BusinessEmployee();
            IEmployee employee = businessEmployee.GetEmployee(int.Parse(employeeId.ToString()));

            return employee.NickName;
        }

        protected string getWorkMode(object workMode)
        {
            if (workMode.ToString() == "P")
                return "Part Time";
            else
                return "Full Time";

        }

        protected void btnDeleteTeamMember_Click(object sender, EventArgs e)
        {
            try
            {

                for (int i = 0; i < gvResults.Rows.Count; i++)
                {
                    CheckBox cb = (CheckBox)gvResults.Rows[i].FindControl("chkShowTeamMember");
                    Label lbl = (Label)gvResults.Rows[i].FindControl("lblTeamMemberId");

                    if (cb.Checked)
                    {
                        string teamMemberId = string.Empty;
                        if (lbl != null)
                        {
                            teamMemberId = lbl.Text;
                            IBusinessTeamMember businessTeamMember = new BusinessTeamMember();
                            businessTeamMember.DeleteTeamMember(Int64.Parse(teamMemberId));
                        }
                    }
                }

                lblMsg.Text = "Deleted";
                BindGridView();
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;
            }
        }

    }


}